<?php
require_once 'Encode.php';
require_once 'boardModel.php';
require_once 'DBConnector.php';
require_once 'view.php';
session_start();

$request = e($_REQUEST['request']);

if($request == null){
	$request = "showMainBoard";
}
$request();

function selectMainBoard($mbIndex=NULL){
	$boardModel = new BoardModel();
	$db = DBConnector::getDB();
	if($mbIndex==NULL){
		$sth = $db->prepare("select * from tMainBoard");
	} else {
		$sth = $db->prepare("select * from tMainBoard where MBIndex={$mbIndex}");
	}
	return $boardModel->select($sth);
}

function showMainBoard(){
	$result = selectMainBoard();
	$view = new View($result);
	$view->render('articleList.php');
}

function showMainBoardArticleDetall(){
	$mbIndex = e($_REQUEST['MBIndex']);
	$boardModel = new BoardModel();
	$boardModel->setTargetTable(BoardModel::TABLE_NAME_MAIN_BOARD);
	if($_REQUEST['MBIndex'] != ''){
		$sth = DBConnector::getDb()->prepare("select * from tMainBoard where MBIndex = ".$mbIndex);
		$data = $boardModel->select($sth);
		$data = $data[0];
		$sth = DBConnector::getDb()->prepare("
				select tmbr.userId,userName, MBRIndex, MBRCreatedDate, MBRContent from tMainBoardResponse as tmbr,
				tUser as tu where tmbr.userId=tu.userId and MBIndex = ".$mbIndex);
		$data1 = $boardModel->select($sth);
		$data['comment'] = $data1;
		$view = new View($data);
		$view->render('article.php');
	}
}

function showAddArticle(){
	$view = new View();
	$view->render('addArticle.php');
}

function deleteMainBoardArticle(){
	if ($_SESSION['token']==e($_REQUEST['token'])) {
		$boardModel = new BoardModel();
		$boardModel->setTargetTable(BoardModel::TABLE_NAME_MAIN_BOARD);
		$boardModel->delete(e($_REQUEST["MBIndex"]));
		showMainBoard();
	}
}

function deleteMainBoardArticleResponse(){
	if ($_SESSION['token']==e($_REQUEST['token'])) {
		$boardModel = new BoardModel();
		$boardModel->setTargetTable(BoardModel::TABLE_NAME_MAIN_BOARD_RESPONSE);
		$boardModel->delete(e($_REQUEST['MBRIndex']));
		$mbIndex = e($_REQUEST['MBIndex']);
		$boardModel->setTargetTable(BoardModel::TABLE_NAME_MAIN_BOARD);
		if(e($_REQUEST['MBIndex']) != ''){
			$sth = DBConnector::getDb()->prepare("select * from tMainBoard where MBIndex = ".$mbIndex);
			$data = $boardModel->select($sth);
			$data = $data[0];
			$sth = DBConnector::getDb()->prepare("select * from tMainBoardResponse as tmbr,
				tUser as tu where tmbr.userId=tu.userId and MBIndex = ".$mbIndex);
			$data1 = $boardModel->select($sth);
			$data['comment'] = $data1;
			$view = new View($data);
			$view->render('article.php');
		}
	}
}

function insertMainBoardArticleResponse(){
	$mbIndex=e($_REQUEST['MBIndex']);
	$userId=$_SESSION['userId'];
	$mbrContent=e($_REQUEST["MBRContent"]);
	$boardModel = new BoardModel();
	$boardModel->setTargetTable(BoardModel::TABLE_NAME_MAIN_BOARD_RESPONSE);
	$boardModel->insert($mbIndex, $userId, $mbrContent);
	showMainBoardArticleDetall();
}

function showMainBoardArticle(){
	$result=selectMainBoard(e($_REQUEST['MBIndex']));
	$view = new View($result[0]);
	$view->render('articleEdit.php');
}

function updateMainBoardArticle(){
	if(e($_REQUEST['MBIndex']) != ""){
		$_mbIndex = e($_REQUEST['MBIndex']);
		$boardModel = new BoardModel();
		$boardModel->setTargetTable(BoardModel::TABLE_NAME_MAIN_BOARD);
		$boardModel->update(e($_REQUEST["MBTitle"]),e($_REQUEST["MBContent"]),$_mbIndex);
		showMainBoardArticleDetall();
	}
}

function insertMainBoardArticle(){
	$boardModel = new BoardModel();
	$boardModel->setTargetTable(BoardModel::TABLE_NAME_MAIN_BOARD);
	$boardModel->insert($_SESSION['userId'],e($_REQUEST["MBTitle"]),e($_REQUEST["MBContent"]));
	$sth = DBConnector::getDb()->prepare("select * from tMainBoard");
	$data = $boardModel->select($sth);
	require_once 'view.php';
	$view = new View($data);
	$view->render('articleList.php');
}

